package com.hanlink.dsp.ui.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C) 秦智科技有限责任公司版权所有: 2015/10/11
 *
 * Description:
 *
 * Revision History
 * Date			         Author		  Description
 * 2015/10/11        fonhal       初始创建
 *
 * </pre>
 */
public class DeviceBroadcastReceiver extends BroadcastReceiver {

    /*   private int mScaningTimes = 0;

       public DeviceBroadcastReceiver(Handler handler){

       }
   */
    @Override
    public void onReceive(Context context, Intent intent) {
        /*String action = intent.getAction();

        //sendNotification(action);
        if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
            //Toast.makeText(context, "begin discovery", Toast.LENGTH_SHORT).show();

        } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
            //Toast.makeText(context, "Discovery device finished", Toast.LENGTH_SHORT).show();
            // DialogHelper.closeProgressDialog(waitingDialog);
            if (++mScaningTimes < 1 && !BtManager.isFounded()) {
                BtManager.startDiscovery();
            } else {
                if (mBluetoothDialog != null && mBluetoothDialog.isShowing() && !BtManager.isFounded()) {
                    mBluetoothDialog.setFail(R.string.bluetooth_discover_fail);
                }
            }
        } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
            //Toast.makeText(context, "my connectLeDevice state changed", Toast.LENGTH_SHORT).show();
        } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            //根据状态来显示蓝牙图标
            if (state == BluetoothAdapter.STATE_ON) {
                BtManager.startDiscovery();
            } else if (state == BluetoothAdapter.STATE_OFF) {
                //Toast.makeText(BtdspActivity.this, "I am closed", Toast.LENGTH_SHORT).show();
            }
        } else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //Toast.makeText(context, "I am connected" + device.getAddress() + ":::" + device.getBondState(), Toast.LENGTH_SHORT).show();
        } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            //Toast.makeText(context, "I am disconnected", Toast.LENGTH_SHORT).show();
        } else if (action.equals(BluetoothDevice.ACTION_FOUND)) { //发现新设备


            BtManager.addCategory(getString(R.string.searched_device));
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            System.out.println(":::发现蓝牙设备" + device);
            //mAdapter.notifyDataSetChanged();
            //TODO
            Log.d("DeviceBroadcastReceiver", device + "::" + device.getName());
            if (device != null && device.getName() != null && device.getName().toLowerCase().contains(DspSettings.DEVICE_NAME)) {
                ClassicDevice d = BtManager.addDevice(device);
                mBluetoothDrawerFragment.refreshList(); //刷新蓝牙列表
                //DialogHelper.closeProgressDialog(waitingDialog);
                if (d != null && d.getBluetoothDevice() != null && !d.isConnected()) {
                    //准备连接蓝牙
                    // Toast.makeText(BtdspActivity.this, "d.getBluetoothDevice().getBondState():" + d.getBluetoothDevice().getBondState(), Toast.LENGTH_SHORT).show();
                        *//*if (waitingDialog != null) {
                            waitingDialog.setMessage("扫描到设备正在连接设备!");
                        }*//*
                    if (mBluetoothDialog != null && mBluetoothDialog.isShowing()) {
                        mBluetoothDialog.setLoading(R.string.bluetooth_to_connect);
                    }
                    try {

                        BtManager.getInstance().connectLeDevice(d);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {

        } else if (action.equals(BtManager.ACTION_CONNECT_SUCCESS)) {
            //设备连接成功
            //
            System.out.println("发送了获取数据命令。。。。。");
            BtManager.getInstance().writeBytes(DspProtocol.GET_MODEL); //获取模式


            found = true;
            ClassicDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (mBluetoothDialog != null && mBluetoothDialog.isShowing()) {
                mBluetoothDialog.setSuccess(R.string.bluetooth_connect_successful);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBluetoothDialog.cancel();
                    }
                }, 3 * 1000);
            }
            // Toast.makeText(BtdspActivity.this, "连接设备成功", Toast.LENGTH_SHORT).show();
            if (BtManager.getInstance().isA2dpConnected(device.getBluetoothDevice())) {
            }
        } else if (action.equals(BtManager.ACTION_CONNECT_FAILED)) {
            //ClassicDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (mBluetoothDialog != null && mBluetoothDialog.isShowing()) {
                mBluetoothDialog.setFail(R.string.bluetooth_connect_fail);
            }
            found = false;
            // Toast.makeText(BtdspActivity.this, "蓝牙连接失败!", Toast.LENGTH_SHORT).show();
        } else if (action.equals(BtManager.ACTION_READ_MODEL)) {


        } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) {
            //Log.d("DeviceBroadcastReceiver", BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
        } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
            int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, 0);
            Log.d("DeviceBroadcastReceiver", BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED + ":::" + state + "::" + BluetoothA2dp.STATE_CONNECTED);
                *//*if (state == BluetoothA2dp.STATE_CONNECTED) {
                }*//*
        }*/
    }

}
